﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace MDIDemo.Modules
{
    public partial class ThuChiTM : DevExpress.XtraEditors.XtraForm
    {
        String sErr = "";
        int idThem = 33;
        int idXoa = 35;
        void SetPermission()
        {
            if (this.Parent.Parent is frmMain)
            {
                frmMain main = (frmMain)this.Parent.Parent;
                btnThem.Visible = frmMain.IsPermis(idThem, main.CurrentPermis.PERMISSION);
                btnXoa.Visible = frmMain.IsPermis(idXoa, main.CurrentPermis.PERMISSION);
                btnLuu.Visible = frmMain.IsPermis(idThem, main.CurrentPermis.PERMISSION);
            }
        }
        public ThuChiTM()
        {
            InitializeComponent();
            rgLoaiPhieu.SelectedIndex = 0;
        }

        private void RefresForm(string p)
        {
            txtTenKH.EditValue = p;
            txtDienThoai.EditValue = p;
            txtDiaChi.EditValue = p;
            txtCMND.EditValue = p;
            txtNgayCap.EditValue = null;
            txtNoiCap.EditValue = p;
            txtSoTien.EditValue = p;
            txtNoiDung.EditValue = p;
            txtCT.EditValue = p;
            txtNgayGD.EditValue = p;
            txtTT.EditValue = p;
            txtTKNo.EditValue = p;
            txtTKCo.EditValue = p;

        }

        private void EnabledForm(bool p)
        {
            rgLoaiPhieu.Enabled = !p;
            txtTenKH.Properties.ReadOnly = !p;
            txtDienThoai.Properties.ReadOnly = !p;
            txtDiaChi.Properties.ReadOnly = !p;
            txtCMND.Properties.ReadOnly = !p;
            txtNgayCap.Properties.ReadOnly = !p;
            txtNoiCap.Properties.ReadOnly = !p;
            txtSoTien.Properties.ReadOnly = !p;
            txtNoiDung.Properties.ReadOnly = !p;

            if (frmMain.__khoaSo)
            {
                btnXoa.Enabled = btnThem.Enabled = false;
            }
            else
            {

                btnXoa.Enabled = btnThem.Enabled = !p;
            }

            btnLuu.Enabled = p;
            btnBoQua.Enabled = p;
            btnTimKiem.Enabled = !p;
            btnIn.Enabled = !p;
        }

        private void ThuChiTM_Load(object sender, EventArgs e)
        {
            EnabledForm(false);
            RefresForm("");
            //btnLuu.Enabled = false;
            //btnBoQua.Enabled = false;
            SetPermission();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            RefresForm("");
            EnabledForm(true);

            BUS.GIAODICHControl ctr = new BUS.GIAODICHControl();
            //DataTable dt = ctr.GetALLByCT(null, null, frmMain.__ngayMoSo, ref sErr);
            //int i = 1;
            //txtCT.Text = (dt.Rows.Count + i).ToString("00000");

            //while (ctr.IsExist(txtCT.Text, null, frmMain.__ngayMoSo))
            //{
            //    txtCT.Text = (dt.Rows.Count + i).ToString("00000");
            //    i++;
            //}
            BUS.CommonControl cmm = new BUS.CommonControl();
            txtCT.Text = cmm.GetSoCTNextNo(frmMain.__ngayMoSo.ToString("yyyy-MM-dd"));

            txtNgayGD.EditValue = frmMain.__ngayMoSo;
            txtTT.EditValue = "Normal";
            txtTenKH.Focus();

        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void rgLoaiPhieu_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (rgLoaiPhieu.EditValue == "PT001")
            {
                txtLoaiCT.EditValue = "PHIẾU THU TIỀN MẶT";
                txtLoaiCT.ForeColor = Color.Green;
            }
            else
            {
                txtLoaiCT.EditValue = "PHIẾU CHI TIỀN MẶT";
                txtLoaiCT.ForeColor = Color.Red;
            }
            txtTenKH.Focus();

        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            sErr = SaveGiaoDich();
            if (sErr != "")
                XtraMessageBox.Show(sErr);
            else
            {
                //     RefresForm("");
                EnabledForm(false);
                XtraMessageBox.Show("Lưu thành công");
            }
        }

        protected String SaveGiaoDich()
        {

            DTO.GIAODICHInfo inf = new DTO.GIAODICHInfo();


            BUS.GIAODICHControl ctr = new BUS.GIAODICHControl();
            //DataTable dt = ctr.GetALLByCT(null, null, frmMain.__ngayMoSo, ref sErr);
            //int i = 1;
            //txtCT.Text = (dt.Rows.Count + i).ToString("00000");

            //while (ctr.IsExist(txtCT.Text, null, frmMain.__ngayMoSo))
            //{
            //    txtCT.Text = (dt.Rows.Count + i).ToString("00000");
            //    i++;
            //}
            BUS.CommonControl cmm = new BUS.CommonControl();
            txtCT.Text = cmm.GetSoCTNextNo(frmMain.__ngayMoSo.ToString("yyyy-MM-dd"));

            inf.SOCT = txtCT.Text;
            inf.LOAICT = rgLoaiPhieu.EditValue.ToString();
            inf.NGAYCT = (DateTime)txtNgayGD.EditValue;
            //     inf.MAKH = IDKH;
            //     inf.TIENHANG = Convert.ToDouble(txtTienHang.EditValue);
            //      inf.PHIVC = Convert.ToDouble(txtPhiVanChuyen.EditValue);
            //     inf.CHIETKHAU = Convert.ToDouble(txtChieutkhau.EditValue);
            inf.TONGTIEN = Convert.ToDouble(txtSoTien.EditValue);
            //       inf.CONGNO = Convert.ToDouble(txtTienThieu.EditValue);
            inf.THUNGAY = Convert.ToDouble(txtSoTien.EditValue);
            inf.TIENMAT = Convert.ToDouble(txtSoTien.EditValue);
            inf.LOAITIEN = "VND";
            inf.NameKH = txtTenKH.EditValue.ToString();
            inf.DIACHIKH = txtDiaChi.Text;
            inf.DIENTHOAI = txtDienThoai.Text;
            inf.NOIDUNG = txtNoiDung.Text;
            inf.TINHTRANG = txtTT.Text;
            inf.INHD = true;
            inf.CREATEBY = frmMain.__userID;
            inf.MODIFIEDBY = frmMain.__userID;
            inf.CREATEDATE = frmMain.__ngayMoSo;
            inf.MODIFIEDDATE = frmMain.__ngayMoSo;

            ctr.Add(inf, ref sErr);

            return sErr;
        }

        private void btnBoQua_Click(object sender, EventArgs e)
        {
            RefresForm("");
            EnabledForm(false);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (txtCT.EditValue != "" && txtNgayGD.EditValue != "")
            {
                BUS.GIAODICHControl ctr = new BUS.GIAODICHControl();
                sErr = ctr.Delete(txtCT.EditValue.ToString(), rgLoaiPhieu.EditValue.ToString(), (DateTime)txtNgayGD.EditValue);
                if (sErr != "")
                    XtraMessageBox.Show(sErr);
                else
                {
                    RefresForm("");
                    EnabledForm(false);
                    XtraMessageBox.Show("Xóa thành công");
                }
            }


        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            Windows.frmViewGiaoDich gd = new Windows.frmViewGiaoDich();
            gd.LoaiCT = rgLoaiPhieu.EditValue.ToString();
            if (gd.ShowDialog(this) == DialogResult.OK)
            {
                DTO.GIAODICHInfo inf = new DTO.GIAODICHInfo(gd.ReturnRow);

                txtCT.Text = inf.SOCT;
                txtNgayGD.EditValue = inf.NGAYCT;
                //    IDKH = inf.MAKH;
                //    txtTienHang.EditValue = inf.TIENHANG;
                //      txtPhiVanChuyen.EditValue = inf.PHIVC;
                //      txtChieutkhau.EditValue = inf.CHIETKHAU;
                txtSoTien.EditValue = inf.TONGTIEN;
                //      txtTienThieu.EditValue = inf.CONGNO;
                //      txtTienDatTruoc.EditValue = inf.THUNGAY;
                txtTenKH.EditValue = inf.NameKH;
                txtDiaChi.Text = inf.DIACHIKH;
                txtDienThoai.Text = inf.DIENTHOAI;
                txtNoiDung.Text = inf.NOIDUNG;
                txtTT.EditValue = inf.TINHTRANG;

                btnThem.Enabled = false;
                EnabledForm(false);


            }
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            BUS.GIAODICHControl ctr = new BUS.GIAODICHControl();
            frmMain main = this.Parent.Parent as frmMain;

            if (txtCT.Text != "" && txtNgayGD.Text != "")
            {
                DataTable dt = ctr.GetALLByCT(txtCT.Text, rgLoaiPhieu.EditValue.ToString(), Convert.ToDateTime(txtNgayGD.EditValue), ref sErr);
                if (rgLoaiPhieu.EditValue == "PT001")
                {
                    main.ShowReport(dt.DataSet, "PTTM", "Phiếu thu tiền mặt");
                }
                else
                {
                    main.ShowReport(dt.DataSet, "PCTM", "Phiếu chi tiền mặt");
                }
            }

        }

        private void ThuChiTM_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                if (btnBoQua.Enabled)
                    btnBoQua_Click(null, null);

            }
            else if (e.KeyCode == Keys.F7)
            {
                if (btnLuu.Enabled)
                    btnLuu_Click(null, null);

            }
        }
    }
}